package javaapplication1.control;

import javaapplication1.dao.PetOwnerDao;
import javaapplication1.dao.PetStoreDao;
import javaapplication1.dao.oracleImpl.PetOwnerDaoOracleImpl;
import javaapplication1.dao.oracleImpl.PetStoreDaoOracleImpl;
import javaapplication1.service.PetOwnerService;
import javaapplication1.service.PetStoreService;
import javaapplication1.service.serviceImpl.PetOwnerServiceImpl;
import javaapplication1.service.serviceImpl.PetStoreServiceImpl;
import javaapplication1.view.MenuView;
import javaapplication1.view.SellPetView;
import javaapplication1.view.SessionScope;
import javaapplication1.model.Pet;
import javaapplication1.model.PetOwner;
import javaapplication1.model.PetStore;

public class SellPetControl {

	private PetOwnerDao petOwnerDao = new PetOwnerDaoOracleImpl();
	private PetStoreDao petStoreDao = new PetStoreDaoOracleImpl();
	private PetOwnerService perOwnerService = new PetOwnerServiceImpl();
	private PetStoreService perStoreService = new PetStoreServiceImpl();

	public void sellPetControl(Pet pet, int id) {
		if (SessionScope.getRole() == 1) {
			PetStore petStore = petStoreDao.retrievePetStore(id);
			if (petStore.getBalance() < pet.getPrice()) {
				System.out.println("出售失败，商店结余不足");
				SellPetView.sellPetView();
			} else {
				perOwnerService.sell(pet, petStore.getId());
				MenuView.menu();
			}
		} else if (SessionScope.getRole() == 2) {
			PetOwner petOwner = petOwnerDao.retrievePetOwner(id);
			if (petOwner.getMoney() < pet.getPrice()) {
				System.out.println("出售失败，用户金额不足");
				SellPetView.sellPetView();
			} else {
				perStoreService.sell(pet, petOwner.getId());
				MenuView.menu();
			}
		}
	}

}